{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a0c82308",
   "metadata": {},
   "source": [
    "# 2.4元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c748389",
   "metadata": {},
   "source": [
    "## 定义：由数个逗号分隔的值组成，如\n",
    "t=(1,2,3)\n",
    "x=(‘c’,’h’,12,’2’,(1,2,3))\n",
    "## 性质\n",
    "* 输出:总是有括号\n",
    "* 输入:可没有括号\n",
    "* 像**字符串**一样，元组是**不可变**的,创建元组**不定长**，**一旦创建后不可改变长度**\n",
    "* 元组可以嵌套，元素可以是任意类型\n",
    "* 元组中只包含一个元素时，需要在元素后面添加**逗号**，否则括号会被当作运算符使用\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "479f571f",
   "metadata": {},
   "source": [
    "#### 元组相加以及长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd7d2122",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 34, 56, 'abc', 'xyz') with length of 5\n"
     ]
    }
   ],
   "source": [
    "tup1 = (12,34,56)\n",
    "tup2 = ('abc','xyz')\n",
    "tup3 = tup1 + tup2\n",
    "print(tup3,\"with length of\",len(tup3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27a41fc7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a63dc57b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 34, 56, 12, 34, 56, 12, 34, 56)\n"
     ]
    }
   ],
   "source": [
    "print(tup1*3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6baa4b3",
   "metadata": {},
   "source": [
    "#### 判断元素是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6b41d62d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print( 3 in (1,3,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0998fd60",
   "metadata": {},
   "source": [
    "#### 迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9f440294",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for x in(1,2,3):print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89b03822",
   "metadata": {},
   "source": [
    "#### 元组中只包含一个元素时，需要在元素后面添加逗号，否则括号会被当作运算符使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "43302c94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = (50)\n",
    "type(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6522e742",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = (50,)\n",
    "type(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54cfae4a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
